rofiles-fuse: Support write/read_buf()
authorAlexander Larsson <alexl@redhat.com>
Tue, 14 Feb 2017 13:36:28 +0000 (14:36 +0100)
committerAtomic Bot <atomic-devel@projectatomic.io>
Tue, 14 Feb 2017 14:59:28 +0000 (14:59 +0000)
These allow us to avoid copying a lot of data around
in userspace. Instead we splice the data directly from
the fd to the destination fd.

Closes: #684
Approved by: cgwalters

src/rofiles-fuse/main.c

index ac44a438f24cf2f47ad873f5558987acec1092eb..3f0832f585a4f541bb5b1dd1543cd81f807747da 100644 (file)
@@ -344,6 +344,26 @@ callback_create(const char *path, mode_t mode, struct fuse_file_info *finfo)
   return do_open (path, mode, finfo);
 }
 
+static int
+callback_read_buf (const char *path, struct fuse_bufvec **bufp,
+                   size_t size, off_t offset, struct fuse_file_info *finfo)
+{
+  struct fuse_bufvec *src;
+
+  src = malloc (sizeof (struct fuse_bufvec));
+  if (src == NULL)
+    return -ENOMEM;
+
+  *src = FUSE_BUFVEC_INIT (size);
+
+  src->buf[0].flags = FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK;
+  src->buf[0].fd = finfo->fh;
+  src->buf[0].pos = offset;
+  *bufp = src;
+
+  return 0;
+}
+
 static int
 callback_read (const char *path, char *buf, size_t size, off_t offset,
               struct fuse_file_info *finfo)
@@ -355,6 +375,19 @@ callback_read (const char *path, char *buf, size_t size, off_t offset,
   return r;
 }
 
+static int
+callback_write_buf (const char *path, struct fuse_bufvec *buf, off_t offset,
+                    struct fuse_file_info *finfo)
+{
+  struct fuse_bufvec dst = FUSE_BUFVEC_INIT (fuse_buf_size (buf));
+
+  dst.buf[0].flags = FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK;
+  dst.buf[0].fd = finfo->fh;
+  dst.buf[0].pos = offset;
+
+  return fuse_buf_copy (&dst, buf, FUSE_BUF_SPLICE_NONBLOCK);
+}
+
 static int
 callback_write (const char *path, const char *buf, size_t size, off_t offset,
                struct fuse_file_info *finfo)
@@ -454,7 +487,9 @@ struct fuse_operations callback_oper = {
   .utime = callback_utime,
   .create = callback_create,
   .open = callback_open,
+  .read_buf = callback_read_buf,
   .read = callback_read,
+  .write_buf = callback_write_buf,
   .write = callback_write,
   .statfs = callback_statfs,
   .release = callback_release,